/**
 * 作者: wgx
 * 日期: 2025-03-31 10:50:37
 * 备注: 用户已解锁的美食攻略
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.skill.Skill;
import com.motu.monstercity.server.game.commondata.restaurant.RestaurantFood;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.PartnerManager;
import com.motu.monstercity.server.game.logic.RestaurantManager;
import com.motu.monstercity.server.game.logic.SkillManager;

public class UserRestaurantFood extends UserRestaurantFoodModel {
    public UserRestaurantFood() {
        super();
    }

    // 记录日志并增加熟练度
    public void addSkillValue(UserInfo userInfo, int num, Object... params) {
        int balance = getSkillValue();// 当前余额
        long dbid = 0;
        MotuLogManager.logItemIncome(userInfo, ItemId.RESTAURANT_FOOD_SKILL_VALUE, dbid, balance, num, params);
        super.addSkillValue(num);
    }

    // 记录日志并扣除熟练度
    public void subSkillValue(UserInfo userInfo, int num, Object... params) {
        int balance = getSkillValue();// 当前余额
        if (num > balance) {
            return;// 余额不足
        }
        long dbid = 0;
        MotuLogManager.logItemConsume(userInfo, ItemId.RESTAURANT_FOOD_SKILL_VALUE, dbid, balance, num, params);
        super.addSkillValue(-num);
    }

    // 一键升级，直到熟练度用完，或是达到满级
    public void levelUpOneKey(PBUserData.Builder pbUserData, UserInfo userInfo) {
        RestaurantFood restaurantFood = RestaurantManager.getRestaurantFood(getFoodId());
        if (restaurantFood == null) {
            return;
        }
        int skillId = restaurantFood.getSkillId();// 技能id，配置在skill表
        Skill skill = SkillManager.getSkill(skillId);
        if (skill == null) {
            return;
        }
        int curLevel = getLevel();
        int maxLevel = skill.getMaxLevel();// 等级上限
        if (curLevel == maxLevel) {
            return;//  已达到最大等级
        }
        int newLevel = 0;
        int skillValue = getSkillValue();// 熟练度
        int totalSubNum = 0;// 总的扣除的熟练度数量
        for (int i=curLevel; i<maxLevel; i++) {
            int subItemNum = (int) skill.takeLevelUpItemNum(i);// 需要消耗的熟练度数量
            //System.out.println("foodid:" + getFoodId() + " subnumL:" + subItemNum + " bal:" + skillValue + " curLevel:" + i);
            if (skillValue >= subItemNum) {
                newLevel = i + 1;
                skillValue = skillValue - subItemNum;
                totalSubNum += subItemNum;//// 总的扣除的熟练度数量
            } else {
                break;// 熟练度不足了
            }
        }
        if (newLevel > 0) {// 有升级过
            subSkillValue(userInfo, totalSubNum, LogType.RESTAURANT_FOOD_UPGRADE);
            putLevel(newLevel);
            update();
            ProtoDataUtils.updatePBUserData(pbUserData, this);
        }
    }
}
